package com.t2c.tibetantochinese.views;

/**
 * Created by zoulu on 16/6/16.
 */

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.t2c.tibetantochinese.R;

public class CustomDialog extends AlertDialog {

    private Context context;
    private String message;
    private String positiveButtonText;
    private String negativeButtonText;
    private DialogInterface.OnClickListener positiveButtonClickListener;
    private DialogInterface.OnClickListener negativeButtonClickListener;
    private DialogType dialogType;

    private String loadingText;
    private int progress = -1;
    private String progress_title;
    private boolean mHasStarted;

    private TextView mTv_PositiveButton;
    private TextView mTv_NegativeButton;
    private View mbuttonSeparator;
    private TextView mTv_Message;
    private TextView mTv_LoadingText;
    private TextView mTv_ProgressText;
    private TextView mTv_ProgressTitle;
    private ProgressBar mProgressBar;

    private LinearLayout mLayout_confirm;
    private LinearLayout mLayout_loading;
    private RelativeLayout mLayout_progress;

    public enum DialogType{
        Confirm,Loading,Progress
    }

    public CustomDialog(Context context,DialogType dialogType) {
        super(context);
        this.context = context;
        this.dialogType = dialogType;
        this.setCanceledOnTouchOutside(false);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.dialog_normal_layout);
        mTv_PositiveButton = (TextView)findViewById(R.id.positiveButton);
        mTv_NegativeButton = (TextView)findViewById(R.id.negativeButton);
        mbuttonSeparator = findViewById(R.id.buttonSeparator);
        mTv_Message = (TextView)findViewById(R.id.message);
        mTv_LoadingText  = (TextView)findViewById(R.id.loading_text);
        mTv_ProgressText = (TextView)findViewById(R.id.progress_text);
        mTv_ProgressTitle = (TextView)findViewById(R.id.progress_title);
        mProgressBar = (ProgressBar) findViewById(R.id.customDialog_progress);

        mLayout_confirm =(LinearLayout)findViewById(R.id.customDialog_message_buttons);
        mLayout_loading = (LinearLayout)findViewById(R.id.content);
        mLayout_progress= (RelativeLayout)findViewById(R.id.customDialog_progress_layout);

        setLayoutGone();

        if(dialogType == DialogType.Confirm){
            setConfirmLayout();
        }else if(dialogType == DialogType.Loading ){
            setLoadingLayout();
        }else if(dialogType == DialogType.Progress){
            setProgressLayout();
        }

    }

    private void setLayoutGone() {
        mLayout_confirm.setVisibility(View.GONE);
        mLayout_loading.setVisibility(View.GONE);
        mLayout_progress.setVisibility(View.GONE);
    }

    private void setProgressLayout() {
        mLayout_progress.setVisibility(View.VISIBLE);

        if (progress != -1) {
            mTv_ProgressText.setText(progress + "%");
            mProgressBar.setProgress(progress);
        } else {
            mTv_ProgressText.setVisibility(View.GONE);
            mProgressBar.setVisibility(View.GONE);
        }
        //设置进度条标题
        if (progress_title != null) {
            mTv_ProgressTitle.setText(progress_title);
        } else {
            mTv_ProgressTitle.setVisibility(View.GONE);
        }
    }

    private void setLoadingLayout() {
        mLayout_loading.setVisibility(View.VISIBLE);

        if (loadingText != null) {
            mTv_LoadingText.setText(loadingText);
        } else {
            mTv_LoadingText.setVisibility(View.GONE);
        }

    }

    private void setConfirmLayout() {
        mLayout_confirm.setVisibility(View.VISIBLE);

        if (positiveButtonText != null) {
            mTv_PositiveButton.setText(positiveButtonText);
            if (positiveButtonClickListener != null) {
                mTv_PositiveButton.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        positiveButtonClickListener.onClick(CustomDialog.this,
                                DialogInterface.BUTTON_POSITIVE);
                    }
                });
            }
        } else {
            mTv_PositiveButton.setVisibility(View.GONE);
            mbuttonSeparator.setVisibility(View.GONE);
        }

        if (negativeButtonText != null) {
            mTv_NegativeButton.setText(negativeButtonText);
            if (negativeButtonClickListener != null) {
                mTv_NegativeButton.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        negativeButtonClickListener.onClick(CustomDialog.this,
                                DialogInterface.BUTTON_NEGATIVE);
                    }
                });
            }
        } else {
            mTv_NegativeButton.setVisibility(View.GONE);
            mbuttonSeparator.setVisibility(View.GONE);
        }

        if (message != null) {
            mTv_Message.setText(message);
        }

    }


    public void setConfirmText(String confirmText) {
        this.message = confirmText;
    }

    /**
     * Set the Dialog progressText
     *
     * @param value
     * @return
     */

    public void setProgress(int value) {
        if (mHasStarted) {
            mProgressBar.setProgress(value);
            mTv_ProgressText.setText(value + "%");
        } else {
            progress = value;
        }
    }

    /**
     * Set the Dialog progressText
     *
     * @param progressText
     * @return
     */
    public void setProgressText(String progressText) {
        if(mTv_ProgressTitle!=null){
            mTv_ProgressTitle.setText(progressText);
        }
        else{
            progress_title = progressText;
        }
    }

    /**
     * Set the Dialog loadingText
     *
     * @param loadingText
     * @return
     */
    public void setLoadingText(String loadingText) {
        this.loadingText = loadingText;
    }

    /**
     * Set the positive button resource and it's listener
     *
     * @param positiveButtonText
     * @return
     */
    public void setPositiveButton(int positiveButtonText,
                                  DialogInterface.OnClickListener listener) {
        this.positiveButtonText = (String) context
                .getText(positiveButtonText);
        this.positiveButtonClickListener = listener;
    }

    public void setPositiveButton(String positiveButtonText,
                                  DialogInterface.OnClickListener listener) {
        this.positiveButtonText = positiveButtonText;
        this.positiveButtonClickListener = listener;
    }

    public void setNegativeButton(int negativeButtonText,
                                  DialogInterface.OnClickListener listener) {
        this.negativeButtonText = (String) context
                .getText(negativeButtonText);
        this.negativeButtonClickListener = listener;
    }

    public void setNegativeButton(String negativeButtonText,
                                  DialogInterface.OnClickListener listener) {
        this.negativeButtonText = negativeButtonText;
        this.negativeButtonClickListener = listener;
    }

    @Override
    protected void onStart() {
        super.onStart();
        mHasStarted = true;
    }


    @Override
    protected void onStop() {
        super.onStop();
        mHasStarted = false;
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getRepeatCount() == 0) {
            //do something...
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}